package com.ruoyi.controller.purchase;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.purchase.MesPurchaseReturn;
import com.ruoyi.service.purchase.IMesPurchaseReturnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购退货Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/purchase/return")
public class MesPurchaseReturnController extends BaseController
{
    @Autowired
    private IMesPurchaseReturnService mesPurchaseReturnService;

    /**
     * 查询采购退货列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesPurchaseReturn mesPurchaseReturn)
    {
        startPage();
        List<MesPurchaseReturn> list = mesPurchaseReturnService.selectMesPurchaseReturnList(mesPurchaseReturn);
        return getDataTable(list);
    }

    /**
     * 查询已完成退货单列表（历史记录）
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:list')")
    @GetMapping("/completedList")
    public TableDataInfo completedList(MesPurchaseReturn mesPurchaseReturn)
    {
        startPage();
        List<MesPurchaseReturn> list = mesPurchaseReturnService.selectCompletedReturnList(mesPurchaseReturn);
        return getDataTable(list);
    }

    /**
     * 导出采购退货列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:export')")
    @Log(title = "采购退货", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesPurchaseReturn mesPurchaseReturn)
    {
        List<MesPurchaseReturn> list = mesPurchaseReturnService.selectMesPurchaseReturnList(mesPurchaseReturn);
        ExcelUtil<MesPurchaseReturn> util = new ExcelUtil<MesPurchaseReturn>(MesPurchaseReturn.class);
        util.exportExcel(response, list, "采购退货数据");
    }

    /**
     * 获取采购退货详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:query')")
    @GetMapping(value = "/{returnId}")
    public AjaxResult getInfo(@PathVariable("returnId") Long returnId)
    {
        return success(mesPurchaseReturnService.selectMesPurchaseReturnByReturnId(returnId));
    }

    /**
     * 新增采购退货
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:add')")
    @Log(title = "采购退货", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesPurchaseReturn mesPurchaseReturn)
    {
        return toAjax(mesPurchaseReturnService.insertMesPurchaseReturn(mesPurchaseReturn));
    }

    /**
     * 修改采购退货
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:edit')")
    @Log(title = "采购退货", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesPurchaseReturn mesPurchaseReturn)
    {
        return toAjax(mesPurchaseReturnService.updateMesPurchaseReturn(mesPurchaseReturn));
    }

    /**
     * 删除采购退货
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:remove')")
    @Log(title = "采购退货", businessType = BusinessType.DELETE)
    @DeleteMapping("/{returnIds}")
    public AjaxResult remove(@PathVariable Long[] returnIds)
    {
        return toAjax(mesPurchaseReturnService.deleteMesPurchaseReturnByReturnIds(returnIds));
    }

    /**
     * 审核采购退货单
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:approve')")
    @Log(title = "审核采购退货", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{returnId}")
    public AjaxResult approve(@PathVariable Long returnId)
    {
        return toAjax(mesPurchaseReturnService.approveMesPurchaseReturn(returnId));
    }

    /**
     * 执行退货
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:execute')")
    @Log(title = "执行退货", businessType = BusinessType.UPDATE)
    @PutMapping("/execute/{returnId}")
    public AjaxResult execute(@PathVariable Long returnId)
    {
        return toAjax(mesPurchaseReturnService.executeMesPurchaseReturn(returnId));
    }

    /**
     * 根据原入库单ID查询退货单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:list')")
    @GetMapping("/receipt/{receiptId}")
    public AjaxResult getReturnListByReceiptId(@PathVariable Long receiptId)
    {
        List<MesPurchaseReturn> list = mesPurchaseReturnService.selectReturnListByReceiptId(receiptId);
        return success(list);
    }

    /**
     * 根据供应商ID查询退货单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:list')")
    @GetMapping("/supplier/{supplierId}")
    public AjaxResult getReturnListBySupplierId(@PathVariable Long supplierId)
    {
        List<MesPurchaseReturn> list = mesPurchaseReturnService.selectReturnListBySupplierId(supplierId);
        return success(list);
    }

    /**
     * 根据仓库ID查询退货单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:list')")
    @GetMapping("/warehouse/{warehouseId}")
    public AjaxResult getReturnListByWarehouseId(@PathVariable Long warehouseId)
    {
        List<MesPurchaseReturn> list = mesPurchaseReturnService.selectReturnListByWarehouseId(warehouseId);
        return success(list);
    }

    /**
     * 根据退货类型查询退货单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:return:list')")
    @GetMapping("/type/{returnType}")
    public AjaxResult getReturnListByType(@PathVariable String returnType)
    {
        List<MesPurchaseReturn> list = mesPurchaseReturnService.selectReturnListByType(returnType);
        return success(list);
    }
}